package com.oocl.customersystem.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.oocl.customersystem.dao.MuserDao;
import com.oocl.customersystem.pojo.Muser;
import com.oocl.customersystem.util.DBUtil;



public class MuserDaoImpl implements MuserDao{

	public String addMuser(Muser m)  throws SQLException {
		String sql="insert into musers (id,icnum,token,name,password,portrait,addr,reg_date,status_1,status_2) values (?,?,?,?,?,?,?,?,?,?)";
		Connection conn=DBUtil.createConnection();
		PreparedStatement pst=null;
		pst=conn.prepareStatement(sql);
		String mid=UUID.randomUUID().toString();
		pst.setString(1, mid);
		pst.setString(2, m.getIcnum());
		pst.setString(3, m.getToken());
		pst.setString(4, m.getMname());
		pst.setString(5, m.getPwd());
		pst.setString(6, m.getPortrait());
		pst.setString(7, m.getMaddr());
		pst.setDate(8, new java.sql.Date(m.getRegistDate().getTime()));
		pst.setString(9, m.getStatus1());
		pst.setString(10, m.getStatus2());
		System.out.println(pst.executeUpdate());
		DBUtil.close(conn, pst, null);
		return mid;
	}

	public List<Muser> findAll() throws SQLException {
		String sql="select * from musers";
		List<Muser> mlist=new ArrayList<Muser>();
		Connection conn=DBUtil.createConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		pst=conn.prepareStatement(sql);
		rs=pst.executeQuery();
		while(rs.next()){
			Muser m=new Muser(
					rs.getString("id"), 
					rs.getString("name"), 
					rs.getString("password"), 
					rs.getString("portrait"), 
					rs.getString("icnum"), 
					rs.getString("addr"), 
					rs.getDate("reg_date"), 
					rs.getString("status_1"), 
					rs.getString("status_2"));
			m.setToken(rs.getString("token"));
			mlist.add(m);
		}
		DBUtil.close(conn, pst, rs);
		return mlist;
	}

	public Muser loadMuser(Muser m) throws SQLException {
		String sql="select * from musers where name=? and password=?";
		Connection conn=DBUtil.createConnection();
		Muser muser=new Muser();
		PreparedStatement pst=null;
		ResultSet rs=null;
		pst=conn.prepareStatement(sql);
		pst.setString(1, m.getMname());
		pst.setString(2, m.getPwd());
		rs=pst.executeQuery();
		while(rs.next()){
			muser=new Muser(
					rs.getString("id"), 
					rs.getString("name"), 
					rs.getString("password"), 
					rs.getString("portrait"), 
					rs.getString("icnum"), 
					rs.getString("addr"), 
					rs.getDate("reg_date"), 
					rs.getString("status_1"), 
					rs.getString("status_2"));
			muser.setToken(rs.getString("token"));
		}
		DBUtil.close(conn, pst, rs);
		return muser;
	}


	public Muser loadMuserById(String mid) throws SQLException {
		String sql="select * from musers where id=?";
		Connection conn=DBUtil.createConnection();
		Muser muser=new Muser();
		PreparedStatement pst=null;
		ResultSet rs=null;
		pst=conn.prepareStatement(sql);
		pst.setString(1, mid);
		rs=pst.executeQuery();
		if(rs.next()){
			muser=new Muser(
					rs.getString("id"), 
					rs.getString("name"), 
					rs.getString("password"), 
					rs.getString("portrait"), 
					rs.getString("icnum"), 
					rs.getString("addr"), 
					rs.getDate("reg_date"), 
					rs.getString("status_1"), 
					rs.getString("status_2"));
			muser.setToken(rs.getString("token"));
		}
		DBUtil.close(conn, pst, rs);
		return muser;
	}

	public Muser loadMuserByIcnum(String icnum) throws SQLException {
		System.out.println("loadMuserByIcnum-----"+icnum);
		String sql="select * from musers where icnum=?";
		Connection conn=DBUtil.createConnection();
		Muser muser=new Muser();
		PreparedStatement pst=null;
		ResultSet rs=null;
		pst=conn.prepareStatement(sql);
		pst.setString(1, icnum);
		rs=pst.executeQuery();
		if(rs.next()){
			muser=new Muser(
					rs.getString("id"), 
					rs.getString("name"), 
					rs.getString("password"), 
					rs.getString("portrait"), 
					rs.getString("icnum"), 
					rs.getString("addr"), 
					rs.getDate("reg_date"), 
					rs.getString("status_1"), 
					rs.getString("status_2")
					);
			muser.setToken(rs.getString("token"));
			System.out.println("token:"+rs.getString("token"));
		}
		DBUtil.close(conn, pst, rs);
		System.out.println("token:---"+muser.getToken());
		return muser;
	}

	public int updateMuserStatus1(Muser muser) throws SQLException {
		System.out.println("updateMuserStatus1:  "+muser);
		String sql="update  musers set status_1 = ?  where icnum = ?";
		int count = -1;
		Connection conn=DBUtil.createConnection();
		PreparedStatement pst=null;
		pst=conn.prepareStatement(sql);
		pst.setString(1, muser.getStatus1());
		pst.setString(2, muser.getIcnum());
		count = pst.executeUpdate();
		DBUtil.close(conn, pst, null);
		return count;
	}

	public int updateMuser(Muser omuser) throws SQLException {
		System.out.println(omuser.toString());
		String sql="update  musers set  icnum= ?,token= ?,name= ?,password= ?,portrait= ?,addr= ?,reg_date= ?,status_1= ?,status_2 = ?  where id = ?";
		int count = -1;
		Connection conn=DBUtil.createConnection();
		PreparedStatement pst=null;
		pst=conn.prepareStatement(sql);
		pst.setString(1, omuser.getIcnum());
		pst.setString(2, omuser.getToken());
		pst.setString(3, omuser.getMname());
		pst.setString(4, omuser.getPwd());
		pst.setString(5, omuser.getPortrait());
		pst.setString(6, omuser.getMaddr());
		pst.setDate(7, new Date(omuser.getRegistDate().getTime()));
		pst.setString(8, omuser.getStatus1());
		pst.setString(9, omuser.getStatus2());
		pst.setString(10, omuser.getMid());
		count = pst.executeUpdate();
		DBUtil.close(conn, pst, null);
		return count;
	}

	public List<Muser> loadMusersDTOsByFtype(String ftype) throws SQLException {
		List<Muser> list = new ArrayList<Muser>();
		System.out.println("loadMusersDTOsByFtype-----"+ftype);
		String sql="select * from musers m,foods f where m.icnum = f.icnum and ftype=?";
		Connection conn=DBUtil.createConnection();
		Muser muser=new Muser();
		PreparedStatement pst=null;
		ResultSet rs=null;
		pst=conn.prepareStatement(sql);
		pst.setString(1, ftype);
		rs=pst.executeQuery();
		while(rs.next()){
			muser=new Muser(
					rs.getString("id"), 
					rs.getString("name"), 
					rs.getString("password"), 
					rs.getString("portrait"), 
					rs.getString("icnum"), 
					rs.getString("addr"), 
					rs.getDate("reg_date"), 
					rs.getString("status_1"), 
					rs.getString("status_2")
					);
			muser.setToken(rs.getString("token"));
			list.add(muser);
		}
		DBUtil.close(conn, pst, rs);
		return list;
	}

}
